package com.study.ju.web;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.study.ju.common.jxl.ViewExcel;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/poi")
public class ExcelController {
    @GetMapping("/downloadEasyExcel")
    public void cooperation(HttpServletRequest request, HttpServletResponse response) {
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
            response.reset();//设置为没有缓存
            response.setContentType("application/x-download");
            response
                    .setHeader("Content-disposition", "attachment; filename="
                            + "11.xls");// 设定输出文件头
            response.setContentType("application/msexcel");// 定义输出类型
            String fileName = new String(("UserInfo " + new SimpleDateFormat("yyyy-MM-dd").format(new Date()))
                    .getBytes(), "UTF-8");
            ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);
            Sheet sheet1 = new Sheet(1, 0);
            sheet1.setSheetName("第一个sheet");
            writer.write0(createTestListObject(), sheet1);
            writer.finish();
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null)
                    out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public static List<List<String>> createTestListObject() {
        List<List<String>> object = new ArrayList<List<String>>();
        for (int i = 0; i < 1000; i++) {
            List<String> da = new ArrayList<String>();
            da.add("字符串" + i);
            object.add(da);
        }
        return object;
    }

    @RequestMapping("/downloadJxlExcel")
    public String downloadTransferTempl(HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        String realPath = request.getSession().getServletContext().getRealPath("/download") + File.separator + "tmpl";
        System.out.println(realPath);
        String fileName = "企业打款模板.xls";
        response.reset();//设置为没有缓存
        response.setContentType("application/x-download");
        OutputStream outputStream = null;
        InputStream inputStream = null;
        try {
            outputStream = response.getOutputStream();
            response.reset();// 清空输出流
            ViewExcel viewExcel = new ViewExcel();
            String fileNameStr = viewExcel.encodeFilename(fileName, request);
            response
                    .setHeader("Content-disposition", "attachment; filename="
                            + fileNameStr);// 设定输出文件头
            response.setContentType("application/msexcel");// 定义输出类型
            // String errPath = FileConfig.getInstance().getImpErrFilePath();
            String errPath = realPath + File.separator;
            inputStream = new FileInputStream(errPath + fileName);
            byte[] bytes = new byte[1024];
            int readBye;
            while ((readBye = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, readBye);
            }
            outputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null)
                    inputStream.close();
                if (outputStream != null)
                    outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @RequestMapping("/sourcePoi")
    public String sourcePoi(HttpSession session, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        String realPath = request.getSession().getServletContext().getRealPath("/download") + File.separator + "tmpl";
        System.out.println(realPath);
        String filename = "sourcePoi.xlsx";
        // 清空response
        response.reset();
        // 设置response的Header
        response.addHeader("Content-Disposition", "attachment;filename="
                + new String(filename.getBytes(),"ISO-8859-1"));
        response.setContentType("application/vnd.ms-excel;charset=gb2312");
        OutputStream outputStream = null;
        InputStream inputStream = null;
        try {
            outputStream = response.getOutputStream();
            // 工作区
            XSSFWorkbook wb = new XSSFWorkbook();
            XSSFSheet sheet = wb.createSheet("test");
            for (int i = 0; i < 1000; i++) {
                // 创建第一个sheet
                // 生成第一行
                XSSFRow row = sheet.createRow(i);
                // 给这一行的第一列赋值
                row.createCell(0).setCellValue("column1");
                // 给这一行的第一列赋值
                row.createCell(1).setCellValue("column2");
                System.out.println(i);
            }
            // 写文件
            wb.write(outputStream);
            // 关闭输出流
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null)
                    inputStream.close();
                if (outputStream != null)
                    outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

}
